#include "MachAlarmReportPanel.h"
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QRandomGenerator>
#include "../../../../network/HttpClient.h"
#include "../../../../models/CommonData.h"
#include "../../../../models/AlarmReportData.h"

MachAlarmReportPanel::MachAlarmReportPanel() {
    initComponent();
    client = new HttpClient();
    connect(client,&HttpClient::responseReceived,this,&MachAlarmReportPanel::onReciveResponse);
}

void MachAlarmReportPanel::initComponent()
{
    const auto layout = new QGridLayout();

    layout->addWidget(initRequsetPanel(),0,0,1,1);
    layout->addWidget(initResponsePanel(),1,0,1,1);
    setLayout(layout);
    setTitle(tr("报警上报"));

}

QWidget *MachAlarmReportPanel::initRequsetPanel()
{
    const auto lbCode = new QLabel(tr("报警代码"));
    const auto lineCode = new QLineEdit();
    const auto lbStatus = new QLabel(tr("设备状态"));
    const auto lineStatus = new QLineEdit();

    const auto btnExecute = new QPushButton(tr("发送请求"));
    const auto lbJson = new QLabel(tr("请求体Json"));
    const auto txtJson = new QTextEdit();
    txtJson->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);

    requestVariableHash.insert("alarmCode",lineCode);
    requestVariableHash.insert("status",lineStatus);
    jsonHash.insert("requestBody",txtJson);

    const auto layout = new QGridLayout();
    layout->addWidget(lbCode,0,0,1,1);
    layout->addWidget(lineCode,0,1,1,1);
    layout->addWidget(lbStatus,1,0,1,1);
    layout->addWidget(lineStatus,1,1,1,1);
    layout->addWidget(lbJson,2,0,1,1);
    layout->addWidget(txtJson,3,0,1,2);
    layout->addWidget(btnExecute,2,1,1,1);

    connect(btnExecute,&QPushButton::clicked,this,&MachAlarmReportPanel::onSendRequest);
    const auto widget = new QWidget();
    widget->setLayout(layout);
    return widget;
}

QWidget *MachAlarmReportPanel::initResponsePanel()
{
    const auto lbJson = new QLabel(tr("响应体json"));
    const auto txtJson = new QTextEdit();
    jsonHash.insert("responseBody",txtJson);
    const auto layout = new QGridLayout();
    layout->addWidget(lbJson,0,0,1,1);
    layout->addWidget(txtJson,1,0,1,1);
    const auto widget = new QWidget();
    widget->setLayout(layout);
    return widget;

}

void MachAlarmReportPanel::onSendRequest()
{
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);
    QString dateTimeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");

    BaseData<CommonData>::execute(&CommonData::setEventTime,dateTimeStr.toStdString());
    BaseData<CommonData>::execute(&CommonData::setTransId,transIdStr.toStdString());

    QJsonObject json;
    json["factoryNo"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo));
    json["transId"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getTransId));
    json["eventTime"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventTime));
    json["eventUser"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser));
    json["machineNo"] = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));

    std::string alarmCode = requestVariableHash["alarmCode"]->text().toStdString();
    std::string status = requestVariableHash["status"]->text().toStdString();


    std::list<AlarmList> alarmList;

    alarmList.push_back({
        alarmCode,status
    });

    QJsonArray alarmListArray;
    for(const auto& item:alarmList)
    {
        QJsonObject obj;
        obj["alarmCode"] = QString::fromStdString(item.alarmCode);
        obj["status"] = QString::fromStdString(item.status);
        alarmListArray.append(obj);
    }
    json["alarmList"]=alarmListArray;

    std::string fullUrl = BaseData<CommonData>::execute(&CommonData::setApiUrl,"/MachAlarmReport");
    client->PostAysnc(QString::fromStdString(fullUrl),json);

    jsonHash["requestBody"]->setPlainText(QJsonDocument(json).toJson());

    qDebug()<<"[MachAlarmReportPanel] request:"<<json;
    qDebug()<<"[MachAlarmReportPanel] url:" <<fullUrl;

}

void MachAlarmReportPanel::onReciveResponse(const QByteArray &json)
{
    qDebug()<<"[MachAlarmReportPanel] response:"<<json;
    QJsonDocument doc = QJsonDocument::fromJson(json);
    jsonHash["responseBody"]->setPlainText(doc.toJson());
}

